package general_algorithms;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Tobias Hardes
 * Aug 5, 2014
 * 1:13:44 PM
 */
public final class Hashing {

	public static byte[] GetHash(Hashing_algorithms algorithm, String input) throws NoSuchAlgorithmException
	{
		String algorithmIdentifier = getHashMessageDigest(algorithm);
		MessageDigest md = MessageDigest.getInstance(algorithmIdentifier);
	

		try {
			md.update(input.getBytes("UTF-8")); //Do we have to change this?!
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return md.digest();
	}
	
	public static byte[] GetCommutativeHash(int x, int y)
	{
		byte[] returnValue = null;
		throw new UnsupportedOperationException();
		//return returnValue;
	}
	
	public static String hashByteToString(byte[] hash)
	{
		//System.out.println(hash);
		BigInteger bigInt = new BigInteger(1, hash);
		return bigInt.toString(16);
	}
		private static String getHashMessageDigest(Hashing_algorithms hash)
		{
			switch (hash) {
			case SHA1:
				return "SHA-1";
			case SHA256:
				return "SHA-256";
			case SHA512:
				return "SHA-512";
			default:
				throw new UnsupportedOperationException();
			}
		}
	
}
